
/* -*- C++ -*- */

//=============================================================================
/*
* @author Feloweria <380374483@qq.com>
* Copyright (C) Feloweria
*
* $ID 2014-07-010 18:04:45 PM
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* @version 1.0
*/
//=============================================================================

#ifndef FELOWRIA_WORK_THREAD_INCLUDE
#define FELOWRIA_WORK_THREAD_INCLUDE


#include <functional>
#include <vector>
#include "Base/DateTime.h"
#include "Thread/Thread.h"
#include "Thread/ThreadMutex.h"

FELOWRIA_BEGIN

#define EVENT_QUEUE_NUM						4
#define EVENT_QUEUE_SIZE					1024 *1024 * 32

extern DLL_EXPORT TimeValue cur_time;

typedef std::function<void()> Event;
typedef std::vector<Event> EventQueue;

class WorkThread;

#ifdef __WINDOWS__
template class DLL_EXPORT std::vector<Event>;
template class DLL_EXPORT std::vector<EventQueue>;
template class DLL_EXPORT std::vector<WorkThread*>;
#endif

class DLL_EXPORT WorkThread : public Thread
{
	
protected:
	WorkThread();
	virtual ~WorkThread();

protected:
	virtual bool Excute();

	virtual bool Update() = 0;

public:
	int32 GetID() const;

public:
	template<typename Object, typename ...Types>
	bool AddEvent(WorkThread *thread, Object *object , void (Object::*func)(Types...), Types...);

	template<typename ...Types>
	bool AddEvent(WorkThread *thread, void (*func)(Types...), Types...);

protected:
	int32 id;

private:
	TimeValue loop_time;

private:
	ThreadMutex recv_event_mutex[EVENT_QUEUE_NUM];
	EventQueue recv_event_queue[EVENT_QUEUE_NUM];
	TimeValue recv_event_time[EVENT_QUEUE_NUM];

	std::vector<EventQueue> send_list;	
	std::vector<WorkThread*> thread_list;
	
	EventQueue event_queue;
};


FELOWRIA_END

#include "WorkThread.inl"

#endif //FELOWRIA_WORK_THREAD_INCLUDE
